<?php
namespace app\admin\controller;
require '../vendor/autoload.php';

use think\Db;
use think\Controller;
class GoodsCategory extends Controller{
    public function index(){
        //查询所有的分类
        $re_category = Db::table('goods_category')->paginate(3);
        $this->assign('category',$re_category);
        return view('index');
    }
    //编辑
    public function edit(){
        if($_POST){//编辑
            $id = $_POST['id'];
            $parent_id = $_POST['cid'];
            $name = $_POST['name'];
            $status = $_POST['status'];
            //如果是父级就不能修改
            $is_parent = Db::table('goods_category')->where(array('parent_id'=>$id))->select();
            if(!empty($is_parent)){
                $this->error('修改失败！','index');
            }
            $res = Db::table('goods_category')->where('id', $id)
                ->update([
                    'name'  => $name,
                    'parent_id' => $parent_id,
                    'status' => $status,
                ]);
            if($res){
                $this->success('修改成功', 'index');
            }
        }else{//添加
            $id = input('id');
            $this->assign('id',$id);
            if($id){//如果存在代表查看某条记录
                $one_category = Db::table('goods_category')->where(array('id'=>$id))->find();
                $this->assign('parent_id',$one_category['parent_id']);
                $this->assign('name',$one_category['name']);
                //查询上级分类名称
                $parent_name = Db::table('goods_category')->where(array('id'=>$one_category['parent_id']))->find();
                $this->assign('parent_name',$parent_name['name']);
            }
            $GoodsCategory = Db::table('goods_category')->where(array('status'=>1))->select();
            $this->assign('nodes',json_encode($GoodsCategory));
            return view('edit');
        }
    }
    //添加
    public function add(){
        if($_POST){ //提交表单
            $parent_id = $_POST['cid'];
            $name = $_POST['name'];
            $status = $_POST['status'];
            $data=array(
                'parent_id'=>$parent_id,
                'name'=>$name,
                'status'=>$status
            );
            $res = Db::table('goods_category')->insert($data);
            if($res){
                $this->success('新增成功', 'index');
            }
        }else{
            $GoodsCategory = Db::table('goods_category')->where(array('status'=>1))->select();
            $this->assign('nodes',json_encode($GoodsCategory));
            return view('edit');
        }
    }
    //删除
    public function delete(){
        $id = input('id');
        //如果是父级就不能修改
        $is_parent = Db::table('goods_category')->where(array('parent_id'=>$id))->select();
        if(empty($is_parent)){
//            $res=Db::table('goods_category')->where(array('id'=>$id))->delete();
            $res = Db::table('goods_category')->where('id',input('id'))->delete();
        }
        if($res){
            $this->success('删除成功！', 'index');
        }else{
            $this->error('删除失败！','index');
        }
    }
}